package com.hxc.shop.controller;

import com.hxc.shop.bean.ShopGoodsInfo;
import com.hxc.shop.common.ResultVO;
import com.hxc.shop.service.GoodsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/goodsInfo")
@CrossOrigin
@Api(tags = {"商品数据处理"})
public class GoodsInfoController {
    @Resource
    private GoodsInfoService goodsInfoService;

    @ApiOperation("新增商品")
    @PostMapping("/addGoods")
    @Transactional(rollbackFor = Exception.class)
    public ResultVO addGoods(@RequestBody ShopGoodsInfo goodsInfo) {
        return goodsInfoService.addGoods(goodsInfo);
    }

    @ApiOperation("更新商品信息")
    @PostMapping("/updateGoods")
    @Transactional(rollbackFor = Exception.class)
    public ResultVO updateGoods(@RequestBody ShopGoodsInfo goodsInfo) {
        return goodsInfoService.updateGoods(goodsInfo);
    }

    @ApiOperation("随机获取自定义个商品数据")
    @GetMapping("/getGoodsByRand")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "count", value="需要获取的商品数量",required = true,paramType = "Integer")
    })
    @Transactional(rollbackFor = Exception.class)
    public ResultVO getGoodsByRand(@RequestParam("count")Integer count) {
        return goodsInfoService.getGoodsByRand(count);
    }

    @ApiOperation("根据商品编号获取商品信息")
    @GetMapping("/getGoodsById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value="商品编号",required = true,paramType = "Integer")
    })
    @Transactional(rollbackFor = Exception.class)
    public ResultVO getGoodsById(@RequestParam("id")Integer id) {
        return goodsInfoService.getGoodsById(id);
    }

    @ApiOperation("搜索商品,根据商品名称查找商品")
    @GetMapping("/searchGoodsByGoodsName")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsName", value="商品名称",required = true,paramType = "String")
    })
    @Transactional(rollbackFor = Exception.class)
    public ResultVO searchGoodsByGoodsName(@RequestParam("goodsName")String goodsName) {
        return goodsInfoService.searchGoodsByGoodsName(goodsName);
    }

    @ApiOperation("根据商品分类Id列表查找商品")
    @GetMapping("/findGoodsByGoodsCategory")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsCategoryId", value="商品分类Id列表,逗号分隔形式",required = true,paramType = "String")
    })
    @Transactional(rollbackFor = Exception.class)
    public ResultVO findGoodsByGoodsCategory(@RequestParam("goodsCategoryId")String goodsCategoryId) {
        return goodsInfoService.findGoodsByGoodsCategory(goodsCategoryId);
    }

    @ApiOperation("根据单个商品分类Id查找商品")
    @GetMapping("/findGoodsByGoodsCategoryId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsCategoryId", value="单个商品分类Id",required = true,paramType = "String")
    })
    @Transactional(rollbackFor = Exception.class)
    public ResultVO findGoodsByGoodsCategoryId(@RequestParam("goodsCategoryId")String goodsCategoryId) {
        return goodsInfoService.findGoodsByGoodsCategoryId(goodsCategoryId);
    }

    @ApiOperation("根据商品分类名称查找商品")
    @GetMapping("/findGoodsByGoodsCategoryName")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsCategoryName", value="商品分类名称",required = true,paramType = "String")
    })
    @Transactional(rollbackFor = Exception.class)
    public ResultVO findGoodsByGoodsCategoryName(@RequestParam("goodsCategoryName")String goodsCategoryName) {
        return goodsInfoService.findGoodsByGoodsCategoryName(goodsCategoryName);
    }

    @ApiOperation("删除商品")
    @GetMapping("/deleteGoodsById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value="商品编号",required = true,paramType = "Integer")
    })
    @Transactional(rollbackFor = Exception.class)
    public ResultVO deleteGoodsById(@RequestParam("id")Integer id) {
        return goodsInfoService.deleteGoodsById(id);
    }



}
